package com.gitee.feizns.quickstart.validation.validator;

import com.gitee.feizns.dynamic.Sets;
import com.gitee.feizns.dynamic.reflect.Constructors;
import com.gitee.feizns.quickstart.validation.DynamicWordsSupplier;
import com.gitee.feizns.quickstart.validation.constraints.DynamicWords;
import com.gitee.feizns.quickstart.validation.constraints.Words;

import java.lang.reflect.Array;
import java.util.Collection;
import java.util.LinkedHashSet;
import java.util.Set;

/**
 * 验证字符、数组{@link Array}或者集合{@link Collection}中的数据是否都在值{@link Words#value()}序列中
 * @author feizns
 * @since 2023/2/7
 */
public class DynamicWordsConstraintValidator extends AbstractAllMatchConstraintValidator<DynamicWords, String> {

    @Override
    protected Set<String> initializeValues(DynamicWords constraintAnnotation) {
        Set<String> values = new LinkedHashSet<>();
        Class<? extends DynamicWordsSupplier>[] classes = constraintAnnotation.value();
        for (Class<? extends DynamicWordsSupplier> aClass : classes)
            values.addAll(Sets.of(Constructors.newInstance(aClass).get()));
        return values;
    }

}
